<?php
class NavigationModel extends Model{
    public function get_admin_nav($id = 0, $field = ''){
        if($id != 0){
            $data = $this->find($id);
            $id = $data['id'];
        }
        if($field !=''){
            $data = $this->field($field)->select();
        }else{
            $data = $this->select();
        }
        // var_dump($data);
        $tree = Datum::entree($data,'id','fid','_',$id);
        // var_dump($tree);
        return $tree;
    }
    public function get_top_nav($root){
        $list = $this->select();
        $nav = [];
        $temp = [];
        $n = [];
        foreach ($list as $key => $value) {
            $temp[$value['id']] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            $fid =  $data['fid'];
            if($fid == $root) {
                $nav[] =& $list[$key];
            }else{
                if(isset($temp[$fid])){
                    $parent =& $temp[$fid];
                    $parent['_'][] =& $list[$key];
                }
            }
        }
        // var_dump($nav[0]['_']);
        foreach($nav as $value){
            if(isset($value['_'])){
                echo '<li class="dropdown">';
                echo '<a href="'.$value['link'].'" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">'.$value['title'].'<span class="caret"></span></a>';
                echo '<ul class="dropdown-menu">';
                foreach($value['_'] as $v){
                    echo '<li><a href="'.$value['link'].'">'.$v['title'].'</a></li>';
                }
                echo '</ul></li>';
            }else{
                echo '<li><a href="'.$value['link'].'">'.$value['title'].'</a></li>';
            }

        }
    }
    
}